#!/bin/bash

# directories
SOURCE="../ffmpeg-2.8"
FAT="out/static-all"
SCRATCH="scratch"
# must be an absolute path
THIN=`pwd`/"thin"

ARCHS="arm64 armv7 i386 x86_64"

COMPILE="y"
LIPO="y"

DEPLOYMENT_TARGET="6.0"

rm -rf $SOURCE/config.h

if [ "$*" ]
then
	if [ "$*" = "lipo" ]
	then
		# skip compile
		COMPILE=
	else
		ARCHS="$*"
		if [ $# -eq 1 ]
		then
			# skip lipo
			LIPO=
		fi
	fi
fi

if [ "$COMPILE" ]
then
	if [ ! `which yasm` ]
	then
		echo 'Yasm not found'
		if [ ! `which brew` ]
		then
			echo 'Homebrew not found. Trying to install...'
                        ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \
				|| exit 1
		fi
		echo 'Trying to install Yasm...'
		brew install yasm || exit 1
	fi
	if [ ! `which gas-preprocessor.pl` ]
	then
		echo 'gas-preprocessor.pl not found. Trying to install...'
		(curl -L https://github.com/libav/gas-preprocessor/raw/master/gas-preprocessor.pl \
			-o /usr/local/bin/gas-preprocessor.pl \
			&& chmod +x /usr/local/bin/gas-preprocessor.pl) \
			|| exit 1
	fi

	CWD=`pwd`
	for ARCH in $ARCHS
	do
		echo "building $ARCH..."
		mkdir -p "$SCRATCH/$ARCH"
		cd "$SCRATCH/$ARCH"

		CFLAGS="-arch $ARCH"
		if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
		then
		    PLATFORM="iPhoneSimulator"
		    CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"
		else
		    PLATFORM="iPhoneOS"
		    CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET -fembed-bitcode"
		    if [ "$ARCH" = "arm64" ]
		    then
		        EXPORT="GASPP_FIX_XCODE5=1"
		    fi
		fi

		XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
		echo $XCRUN_SDK
		CC="xcrun -sdk $XCRUN_SDK clang"
		CXXFLAGS="$CFLAGS"
		LDFLAGS="$CFLAGS"

		TMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \
		    --target-os=darwin \
		    --arch=$ARCH \
		    --cc="$CC" \
		    --extra-cflags="$CFLAGS" \
		    --extra-ldflags="$LDFLAGS" \
		    --prefix="$THIN/$ARCH" \
		    --enable-cross-compile --enable-pic \
				--enable-gpl --enable-nonfree --enable-version3 --enable-small \
				--disable-doc --disable-programs --disable-debug --disable-ffmpeg --disable-ffprobe --disable-ffplay --disable-ffserver \
				--disable-postproc --disable-avdevice  --disable-avfilter  --disable-swresample --disable-swscale \
				--disable-everything \
				--enable-decoder=h264 --enable-decoder=aac --enable-decoder=mjpeg \
				--enable-muxer=mp4 --enable-muxer=flv --enable-muxer=hls --enable-muxer=mov --enable-muxer=mjpeg --enable-muxer=image2 \
				--enable-demuxer=mov --enable-demuxer=h264 --enable-demuxer=mpegts \
				--enable-demuxer=flv --enable-demuxer=hls --enable-demuxer=image2 \
				--enable-demuxer=image_jpeg_pipe --enable-demuxer=mjpeg \
				--enable-demuxer=rtsp --enable-demuxer=rtp \
				--enable-parser=aac \
				--enable-parser=h264 \
				--enable-bsf=aac_adtstoasc --enable-bsf=h264_mp4toannexb \
				--enable-protocol=file --enable-protocol=hls --enable-protocol=http --enable-protocol=https --enable-protocol=rtp --enable-protocol=tcp --enable-protocol=udp \
				--enable-protocol=rtmp \
		|| exit 1

		make -j3 install $EXPORT || exit 1
		cd $CWD
	done
fi

if [ "$LIPO" ]
then
	echo "building fat binaries..."
	mkdir -p $FAT/lib
	set - $ARCHS
	CWD=`pwd`
	cd $THIN/$1/lib
	for LIB in *.a
	do
		cd $CWD
		echo lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB 1>&2
		lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1
	done

	cd $CWD
	cp -rf $THIN/$1/include $FAT
fi

echo Done
